<template>
	<view class="container">
		<view class="module-list">
			<view class="module-list-item" @click="requsetIgnoreBattery">
				<view class="module-list-item-title">电池优化</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
			<view class="module-list-item" @click="startForegroundService">
				<view class="module-list-item-title">开启前台服务</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
			<view class="module-list-item" @click="stopForegroundService">
				<view class="module-list-item-title">关闭前台服务</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
			<view class="module-list-item" @click="startPermissionSetting">
				<view class="module-list-item-title">权限设置</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
			<view class="module-list-item" @click="checkAutoRecordEnableStatus">
				<view class="module-list-item-title">检测自动录音开启状态</view>
				<view class="module-list-item-value">{{autoFlagText}}</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
			<view class="module-list-item">
				<view class="module-list-item-title">检测SIM卡1</view>
				<view class="module-list-item-value">{{sim1Number ? sim1Number : '不存在'}}</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
			<view class="module-list-item">
				<view class="module-list-item-title">检测SIM卡2</view>
				<view class="module-list-item-value">{{sim2Number ? sim2Number : '不存在'}}</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
			<view class="module-list-item" @click="getCallLogList">
				<view class="module-list-item-title">获取通话记录并携带录音文件</view>
				<uni-icons type="forward" size="16" color="#999"></uni-icons>
			</view>
		</view>

		<view class="write-box">
			<input v-model="phoneNumber" type="number" placeholder="请输入手机号码" />
			<button class="send-button" type="primary" @click="makePhoneCall">拨号</button>
		</view>

		<view class="call-log-box">
			<view class="title">当前通话数据：</view>
			<view class="value">{{callLog}}</view>
		</view>

		<view class="remark-box">
			<view class="title">插件使用说明：</view>
			<view>1. 打开manifest.json文件，重新获取APPID</view>
			<view>2. 在插件页面选择试用，再选择示例项目</view>
			<view>3. 在manifest.json文件中，选择【APP原生插件配置】->【选择云端插件】，然后勾选本插件</view>
			<view>4. 打包自定义基座</view>
			<view>5. 运行到Android真机设备调试</view>
		</view>
	</view>
</template>

<script>
	// #ifdef APP-PLUS
	const muCall = uni.requireNativePlugin('Mu-Call');
	// #endif

	export default {
		data() {
			return {
				autoFlag: -1,
				autoFlagText: '未检测',
				sim1Number: '',
				sim2Number: '',
				phoneNumber: '',
				callLog: ''
			}
		},
		watch: {
			callLog: {
				handler(newValue, oldValue) {
					if (newValue) {
						const ruselt = JSON.parse(newValue)
						if (ruselt.recordFile) {
							this.uploadList(ruselt)
						}

					} else {
						console.error('newValue is not an array or is empty');
					}
				},
				deep: true // 深度监听数组内部的变化   
			}
		},

		created() {
			// #ifdef APP-PLUS
			var recordFolderTemp = plus.io.convertLocalFileSystemURL("_www");
			muCall.init({
				simId: -1,
				count: 10,
				saveFlag: true,
				recordFolderTemp
			}, res => {
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			})
			if (muCall.hasSimCard(0)) {
				this.sim1Number = muCall.getSimInfo(0).number;
			}

			if (muCall.hasSimCard(1)) {
				console.log(muCall.getSimInfo(1))
				this.sim2Number = muCall.getSimInfo(1).number;
			}

			// TODO 注册通话监听
			muCall.registerCallListener(res => {
				this.callLog = JSON.stringify(res)
			})
			// #endif
		},
		onShow() {
			// #ifdef APP-PLUS
			this.autoFlag = muCall.checkAutoRecordEnableStatus();
			if (this.autoFlag == 0) {
				this.autoFlagText = '未开启'
			} else if (this.autoFlag == 1) {
				this.autoFlagText = '已开启'
			} else {
				this.autoFlagText = '检测异常'
			}
			// #endif
		},
		methods: {
			UploadCall(newValue, title) { //上传通话记录
				let that = this;
				if (newValue && newValue.data) {
					this.request('/outcalllog/outCallLog/add', 'post', {
						dialingTime: newValue.data.talkTime, //拨号时间
						dialEndingTime: newValue.data.lastModified, //拨号结束时间
						makingPhoneCalls: newValue.data.number, //拨打电话
						nativePhone: newValue.data.simInfo.number, //本机号码
						soundRecordUrl: title //录音文件
					}).then(res => {
						console.log(res.data);
						if (res.data.code != 200) {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						} else {

						}
					})
				}
			},
			uploadList(newValue) { //上传音频
				const that = this
				const api = 'http://192.168.1.16:8080/jeecg-boot/sys/common/uploadLocalAly';
				// 遍历newValue数组中的每个文件项  

				// 检查item是否有recordFile属性  
				if (newValue && newValue.recordFile) {
					muCall.uploadFile({
							url: api,
							filePath: newValue.recordFile.path,
							name: "file",
							header: {
								// 请求头
							},
							formData: {
								biz: "scott/pic",
								file: newValue.recordFile.path
								// 请求表单数据
							},
							timeout: 60000 // 响应超时时间, 单位ms
						}, res => {
							const result = JSON.parse(res.data)
							let {
								code, // 响应码
								message, // 响应消息
								success, // 接口响应数据
								result: resultinfo //返回结果重命名
							} = result
							if (code == 200) {
								that.UploadCall(newValue, resultinfo)
							} else {
								uni.showToast({
									title: message,
									icon: 'none'
								})
							}
							console.log(result);
						}

					)
				} else {

				}

			},
			requsetIgnoreBattery() {
				muCall.requsetIgnoreBattery();
			},
			startForegroundService() {
				muCall.startForegroundService();
			},
			stopForegroundService() {
				muCall.stopForegroundService();
			},
			startPermissionSetting() {
				muCall.startPermissionSetting();
			},
			checkAutoRecordEnableStatus() {
				if (this.autoFlag == 1) {
					uni.showToast({
						title: "已开启",
						icon: 'none'
					})
				} else {
					if (!muCall.startSystemRecordSetting()) {
						uni.showToast({
							title: "请前往手机系统拨号软件设置",
							icon: 'none'
						})
					}
				}
			},
			getCallLogList() {
				uni.navigateTo({
					url: '/pages/call-log/call-log'
				})
			},
			makePhoneCall() {
				if (!this.phoneNumber) {
					uni.showToast({
						title: "请输入手机号码",
						icon: 'none'
					})
					return
				}

				muCall.makePhoneCall(this.phoneNumber)
			}
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
	}

	.container {
		padding: 30rpx;
		box-sizing: border-box;
	}

	.module-list {
		background: #fff;
		border-radius: 10rpx;
		padding: 10rpx 0;
		box-sizing: border-box;
	}

	.module-list .module-list-item {
		display: flex;
		align-items: center;
		padding: 20rpx;
		box-sizing: border-box;
		border-bottom: 1px solid #EBEBEB;
	}

	.module-list .module-list-item:last-child {
		border-bottom: none;
	}

	.module-list-item .module-list-item-title {
		flex: 1;
		font-size: 28rpx;
		color: #666;
	}

	.module-list-item .module-list-item-value {
		font-size: 24rpx;
		color: #333;
		margin: 0 10rpx;
	}

	.write-box {
		display: flex;
		align-items: center;
		background: #fff;
		border-radius: 10rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin-top: 30rpx;
	}

	.write-box input {
		flex: 1;
		margin-right: 20rpx;
		color: #333;
		font-size: 28rpx;
	}

	.send-button {
		width: 120rpx;
		height: 60rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 60rpx;
	}

	.call-log-box {
		margin-top: 20rpx;
		color: #999;
		font-size: 24rpx;
		box-sizing: border-box;
	}

	.call-log-box .value {
		background-color: #fff;
		padding: 20rpx;
		box-sizing: border-box;
		border-radius: 10rpx;
		min-height: 300rpx;
		margin-top: 10rpx;
		white-space: pre-wrap;
		word-break: break-all;
	}

	.remark-box {
		margin-top: 30rpx;
		color: #999;
		font-size: 24rpx;
		border-top: 1px solid #eee;
		padding-top: 20rpx;
		box-sizing: border-box;
	}

	.remark-box view {
		margin-left: 20rpx;
		margin-bottom: 10rpx;
	}

	.title {
		color: #666;
		margin-left: 0 !important;
	}
</style>